<?php

namespace Respect;

use DirectoryIterator;
use ArrayObject;

class Loader
{

    protected $config = array(
        'Respect' => array(
            'library_path' => '../library',
            'public_path' => '../public',
        )
    );

    public static function go($configPath)
    {
        return new static($configPath);
    }

    public function loadConfig($configPath)
    {
        foreach (new DirectoryIterator($configPath) as $configFile) {
            if (!$configFile->isFile())
                continue;
            $this->config = array_merge(
                    $this->config,
                    parse_ini_file($configFile->getRealPath(), true)
            );
        }
    }

    public function __construct($configPath)
    {
        $this->loadConfig($configPath);
        set_include_path($this->config['Respect']['library_path'] . PATH_SEPARATOR . get_include_path());
        spl_autoload_register(array($this, 'loadClass'));
        $core = new StackedRestfulCore\Core('\Acme\\Explosives');
    }

    public function loadClass($className)
    {
        if (false !== strpos($className, '\\')) {
            $className = trim($className, '\\');
            require_once str_replace('\\', '/', $className) . '.php';
        } else {
            require_once str_replace('_', '/', $className) . '.php';
        }
    }

}